﻿using KPDownloadAutoTask;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Text;


namespace Jmit.Deyi.AutoTask
{
    public class AppConfigManager
    {

        public static int trancountid = 0;


        public static AppConfig appConfig = new AppConfig();
        public static AppConfig LoadConfig()
        {
            
            appConfig.dbConn_wms = System.Configuration.ConfigurationManager.AppSettings["dbConn_wms"].ToString();
            appConfig.dbConn_dps = System.Configuration.ConfigurationManager.AppSettings["dbConn_dps"].ToString();
            appConfig.down_minute = System.Configuration.ConfigurationManager.AppSettings["down_minute"].ToString();
            appConfig.up_minute = System.Configuration.ConfigurationManager.AppSettings["up_minute"].ToString();
            appConfig.version = System.Configuration.ConfigurationManager.AppSettings["version"].ToString();

            return appConfig;

            /*else
            {
                SaveConfig();
            }*/
        }

        public static void SaveConfig(AppConfig _deyiConfig,string path = "")
        {
            //获取Configuration对象
            Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            //写入元素的Value
            config.AppSettings.Settings["dbConn_wms"].Value = appConfig.dbConn_wms;
            config.AppSettings.Settings["dbConn_dps"].Value = appConfig.dbConn_dps;
            config.AppSettings.Settings["down_minute"].Value = appConfig.down_minute;
            config.AppSettings.Settings["up_minute"].Value = appConfig.up_minute;
            config.AppSettings.Settings["version"].Value = appConfig.version;
            
            //增加元素
            //config.AppSettings.Settings.Add("url", "http://www.myhack58.com");
            //删除元素
            //config.AppSettings.Settings.Remove("name");
            
            //一定要记得保存，写不带参数的config.Save()也可以
            config.Save(ConfigurationSaveMode.Modified);
            //刷新，否则程序读取的还是之前的值（可能已装入内存）
            System.Configuration.ConfigurationManager.RefreshSection("appSettings");
        }



    }
}
